package com.hzhr.iot.cqgas.ds.controller;

import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.core.util.HzhrDateUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.cqgas.ds.dao.device.custom.DevCollectDataCustomDao;
import com.hzhr.iot.cqgas.ds.dao.device.example.*;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.custom.SmCollectDataCustomDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.custom.SmDeviceCustomDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.custom.SmGasUserCustomDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.example.SmMeterDao;
import com.hzhr.iot.cqgas.ds.dao.smartmeter.example.SmNodeNetworkDao;
import com.hzhr.iot.cqgas.ds.dao.toll.custom.TollGasUserCustomDao;
import com.hzhr.iot.cqgas.ds.dao.toll.example.TollGasUserDao;
import com.hzhr.iot.cqgas.ds.dao.user.example.UsrCompanyDao;
import com.hzhr.iot.cqgas.ds.dao.user.example.UsrDeptDao;
import com.hzhr.iot.cqgas.ds.dao.user.example.UsrUserDao;
import com.hzhr.iot.cqgas.ds.entity.device.*;
import com.hzhr.iot.cqgas.ds.entity.toll.TollGasUserEntity;
import com.hzhr.iot.cqgas.ds.entity.toll.TollGasUserExample;
import com.hzhr.iot.cqgas.ds.entity.user.UsrCompanyEntity;
import com.hzhr.iot.cqgas.ds.entity.user.UsrDeptEntity;
import com.hzhr.iot.cqgas.ds.entity.user.UsrDeptExample;
import com.hzhr.iot.cqgas.ds.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.Future;

@RestController
public class CollectingDataController {
    @Autowired
    CollectingDataService collectingDataService;
    @Autowired
    DevIotDeviceDao devIotDeviceDao;
    @Autowired
    DevCollectDeviceDao devCollectDeviceDao;
    @Autowired
    DevOptionsService devOptionsService;
    @Autowired
    StatisService statisService;
    @Autowired
    TollGasUserDao tollGasUserDao;
    @Autowired
    UsrDeptDao usrDeptDao;
    @Autowired
    CompanyService companyService;
    @Autowired
    UsrCompanyDao usrCompanyDao;
    @Autowired
    ProdFactoryService prodFactoryService;
    @Autowired
    DevProdFactoryDao devProdFactoryDao;
    @Autowired
    DeptService deptService;
    @Autowired
    CollectDeviceTypeService collectDeviceTypeService;
    @Autowired
    DevCollectDeviceTypeDao devCollectDeviceTypeDao;
    @Autowired
    IotDeviceTypeService iotDeviceTypeService;
    @Autowired
    DevIotDeviceTypeDao devIotDeviceTypeDao;
    @Autowired
    GasUserService gasUserService;
    @Autowired
    UserService userService;
    @Autowired
    UsrUserDao usrUserDao;
    @Autowired
    IotDeviceService iotDeviceService;
    @Autowired
    CollectDeviceService collectDeviceService;
    @Autowired
    SmDeviceCustomDao smDeviceCustomDao;
    @Autowired
    SmMeterDao smMeterDao;
    @Autowired
    SmNodeNetworkDao smNodeNetworkDao;
    @Autowired
    SmCollectDataCustomDao smCollectDataCustomDao;
    @Autowired
    DevCollectDataDao devCollectDataDao;
    @Autowired
    DevCollectDataCustomDao devCollectDataCustomDao;
    @Autowired
    TollGasUserCustomDao tollGasUserCustomDao;
    @Autowired
    SmGasUserCustomDao smGasUserCustomDao;
    @Autowired
    TollOptionsService tollOptionsService;
    @Value("${companyId}")
    String companyId;

    private Logger logger = LoggerFactory.getLogger(CollectingDataController.class);

    @RequestMapping(value = "/syncCollectingData")
    public void syncCollectingData(@RequestParam("day") Integer day) throws Exception {
        int syncCount = 0;
        long start = new Date().getTime();

        Date date1 = day != null ? HzhrDateUtil.addDay(new Date(), -day) : null;
        Date date2 = date1 != null ? new Date() : null;

        //查询出采集设备
        List<DevCollectDeviceEntity> devCollectDeviceEntities = devCollectDeviceDao.selectByExample(null);
        Map<String, Integer> collectDevices = new HashMap<>();
        for (DevCollectDeviceEntity devCollectDeviceEntity :
                devCollectDeviceEntities) {
            collectDevices.put(devCollectDeviceEntity.getOutId(), devCollectDeviceEntity.getId());
        }
        //查询出物联网设备
        List<DevIotDeviceEntity> devIotDeviceEntities = devIotDeviceDao.selectByExample(null);
        Map<String, Integer> iotDevices = new HashMap<>();
        for (DevIotDeviceEntity devIotDeviceEntity :
                devIotDeviceEntities) {
            iotDevices.put(devIotDeviceEntity.getOutId(), devIotDeviceEntity.getId());
        }

        //同步数据
        List<Future<Integer>> lstFuture = new ArrayList<Future<Integer>>();// 存放所有的线程，用于获取结果
        for (int i = 1; i <= 10; i++) {
            Future<Integer> result = collectingDataService.syncCollectingData(i, date1, date2, collectDevices, iotDevices);
            lstFuture.add(result);
        }
        for (Future<Integer> future : lstFuture) {// 获取值。get是阻塞式，等待当前线程完成才返回值
            syncCount += future.get();
        }

        long end = new Date().getTime();
        logger.info("同步采集数据结束: " + (end - start) / 1000 + "s");
        logger.info("同步时间: " + (end - start) / 1000 + "s");
        logger.info("同步数量: " + syncCount + "条");
    }

    @RequestMapping(value = "/syncCollectingData_day")
    public void syncCollectingData_day() throws Exception {
        int syncCount = 0;
        long start = new Date().getTime();

        Date date1 = HzhrDateUtil.addDay(new Date(), -1);
        Date date2 = new Date();

        //查询出采集设备
        List<DevCollectDeviceEntity> devCollectDeviceEntities = devCollectDeviceDao.selectByExample(null);
        Map<String, Integer> collectDevices = new HashMap<>();
        for (DevCollectDeviceEntity devCollectDeviceEntity :
                devCollectDeviceEntities) {
            collectDevices.put(devCollectDeviceEntity.getOutId(), devCollectDeviceEntity.getId());
        }
        //查询出物联网设备
        List<DevIotDeviceEntity> devIotDeviceEntities = devIotDeviceDao.selectByExample(null);
        Map<String, Integer> iotDevices = new HashMap<>();
        for (DevIotDeviceEntity devIotDeviceEntity :
                devIotDeviceEntities) {
            iotDevices.put(devIotDeviceEntity.getOutId(), devIotDeviceEntity.getId());
        }

        //同步数据
        List<Future<Integer>> lstFuture = new ArrayList<Future<Integer>>();// 存放所有的线程，用于获取结果
        for (int i = 1; i <= 10; i++) {
            Future<Integer> result = collectingDataService.syncCollectingData(i, date1, date2, collectDevices, iotDevices);
            lstFuture.add(result);
        }
        for (Future<Integer> future : lstFuture) {// 获取值。get是阻塞式，等待当前线程完成才返回值
            syncCount += future.get();
        }

        long end = new Date().getTime();
        logger.info("同步采集数据结束: " + (end - start) / 1000 + "s");
        logger.info("同步时间: " + (end - start) / 1000 + "s");
        logger.info("同步数量: " + syncCount + "条");
    }

    @RequestMapping({"/syncDtuData_5s"})
    public void syncDtuData_5s() {
        (new Date()).getTime();


        List<DevIotDeviceEntity> dtus = new ArrayList<DevIotDeviceEntity>();
        Map<String, Object> criteriaMap = new HashMap<String, Object>();
        criteriaMap.put("startDate1", HzhrDateUtil.dateToStr(HzhrDateUtil.getDayBegin()));
        List<Map<String, Object>> iotdevices = this.smDeviceCustomDao.selectIotDevice1(criteriaMap);
        String dtuCategoryCode = this.devOptionsService.getCode("IOT_DEVICE_CATEGORY", "DTU");
        String gprsCategoryCode = this.devOptionsService.getCode("IOT_DEVICE_CATEGORY", "GPRS_IOT_GAS_METER");
        String ccrCategoryCode = this.devOptionsService.getCode("IOT_DEVICE_CATEGORY", "CONCENTRATOR");
        String pulseCategoryCode = this.devOptionsService.getCode("IOT_DEVICE_CATEGORY", "PULSE_READING");
        String iotPlatformCode = this.devOptionsService.getCode("IOT_PLATFORM_TYPE", "SELF_BUILD");

        List<DevIotDeviceTypeEntity> devIotDeviceTypeEntities = this.devIotDeviceTypeDao.selectByExample(null);
        Map<String, Integer> dtuIotDeviceTypes = new HashMap<String, Integer>();
        Map<String, Integer> gprsIotDeviceTypes = new HashMap<String, Integer>();
        Map<String, Integer> pulseIotDeviceTypes = new HashMap<String, Integer>();
        Map<String, Integer> ccrIotDeviceTypes = new HashMap<String, Integer>();

        if (devIotDeviceTypeEntities.size() > 0) {
            DevIotDeviceTypeEntity devIotDeviceTypeEntity = devIotDeviceTypeEntities.get(0);
            if (dtuCategoryCode.equals(devIotDeviceTypeEntity.getDeviceCategoryCode())) {
                dtuIotDeviceTypes.put(devIotDeviceTypeEntity.getOutId(), devIotDeviceTypeEntity.getId());
            }  if (gprsCategoryCode.equals(devIotDeviceTypeEntity.getDeviceCategoryCode())) {
                gprsIotDeviceTypes.put(devIotDeviceTypeEntity.getOutId(), devIotDeviceTypeEntity.getId());
            }  if (pulseCategoryCode.equals(devIotDeviceTypeEntity.getDeviceCategoryCode())) {
                pulseIotDeviceTypes.put(devIotDeviceTypeEntity.getOutId(), devIotDeviceTypeEntity.getId());
            }  if (ccrCategoryCode.equals(devIotDeviceTypeEntity.getDeviceCategoryCode())) {
                ccrIotDeviceTypes.put(devIotDeviceTypeEntity.getOutId(), devIotDeviceTypeEntity.getId());
            }
        }

        List<UsrDeptEntity> usrDeptEntities = this.usrDeptDao.selectByExample(null);
        Map<String, UsrDeptEntity> userDepts = new HashMap<String, UsrDeptEntity>();

        if (usrDeptEntities.size() > 0) {
            UsrDeptEntity userDeptEntity = usrDeptEntities.get(0);
            userDepts.put(userDeptEntity.getCode(), userDeptEntity);
        }


        List<UsrCompanyEntity> usrCompanyEntities = this.usrCompanyDao.selectByExample(null);
        Map<String, Integer> usrCompanys = new HashMap<String, Integer>();

        Iterator iterator1 = usrCompanyEntities.iterator();
        while (iterator1.hasNext()) {
            UsrCompanyEntity usrCompanyEntity = (UsrCompanyEntity)iterator1.next();
            usrCompanys.put(usrCompanyEntity.getOutId(), usrCompanyEntity.getId());
        }


        iterator1 = iotdevices.iterator();
        while (iterator1.hasNext()) {
            Map<String, Object> iotdevice = (Map)iterator1.next();
            try {
                String nodeId = HzhrTypeUtil.object2string(iotdevice.get("NODE_ID"));
                HzhrTypeUtil.object2string(iotdevice.get("EQ_ID"));
                String eqNo = HzhrTypeUtil.object2string(iotdevice.get("EQ_NO"));
                String des = HzhrTypeUtil.object2string(iotdevice.get("INSTALL_ADDR"));
                HzhrTypeUtil.object2string(iotdevice.get("MT_ID"));
                String dtutId = HzhrTypeUtil.object2string(iotdevice.get("DTUT_ID"));
                HzhrTypeUtil.object2string(iotdevice.get("CCT_ID"));
                String orgId = HzhrTypeUtil.object2string(iotdevice.get("ORG_ID"));
                HzhrTypeUtil.object2string(iotdevice.get("MPR_ID"));
                String dPrId = HzhrTypeUtil.object2string(iotdevice.get("DPR_ID"));
                HzhrTypeUtil.object2string(iotdevice.get("CPR_ID"));
                String deptCode = orgId.replace("0000", "9-01");
                String resourceCode = orgId.replace("0000", "01000901");

                if (dtutId != null) {
                    DevIotDeviceExample devIotDeviceExample = new DevIotDeviceExample();
                    devIotDeviceExample.createCriteria().andOutIdEqualTo(nodeId);
                    List<DevIotDeviceEntity> devIotDeviceEntities = this.devIotDeviceDao.selectByExample(devIotDeviceExample);

                    if (devIotDeviceEntities.size() == 0) {
                        DevIotDeviceEntity devIotDeviceEntity = new DevIotDeviceEntity();
                        devIotDeviceEntity.setOutId(nodeId);
                        devIotDeviceEntity.setCode(eqNo);
                        devIotDeviceEntity.setDes(des);
                        devIotDeviceEntity.setCategoryCode(dtuCategoryCode);
                        devIotDeviceEntity.setTypeId((Integer)dtuIotDeviceTypes.get(dtutId));

                        Integer iotCompanyId = (Integer)usrCompanys.get(dPrId);
                        devIotDeviceEntity.setCompanyId(Integer.valueOf((iotCompanyId != null) ? iotCompanyId.intValue() : 1));

                        UsrDeptEntity usrDeptEntity = (UsrDeptEntity)userDepts.get(deptCode);
                        if (usrDeptEntity != null) {
                            devIotDeviceEntity.setDeptId(usrDeptEntity.getId());
                        } else {
                            devIotDeviceEntity.setDeptId(Integer.valueOf(0));
                        }

                        devIotDeviceEntity.setGasCompanyId(Integer.valueOf(Integer.parseInt(this.companyId)));
                        devIotDeviceEntity.setIotPlatformCode(iotPlatformCode);
                        devIotDeviceEntity.setIotPlatformDeviceId(eqNo);
                        devIotDeviceEntity.setCreateTime(new Date());
                        devIotDeviceEntity.setCreateUserId(Integer.valueOf(0));
                        devIotDeviceEntity.setResourceCode(resourceCode);
                        devIotDeviceEntity.setIsValid(CommonOptions.YES_NO.YES);
                        devIotDeviceEntity.setState("1");
                        this.devIotDeviceDao.insert(devIotDeviceEntity);
                        dtus.add(devIotDeviceEntity); continue;
                    }
                    dtus.add((DevIotDeviceEntity)devIotDeviceEntities.get(0));
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        List<DevCollectDeviceEntity> flowMetres = new ArrayList<DevCollectDeviceEntity>();
        criteriaMap = new HashMap<String, Object>();
        criteriaMap.put("startDate", HzhrDateUtil.dateToStr(HzhrDateUtil.getDayBegin()));
        List<Map<String, Object>> collectDevices = this.smDeviceCustomDao.selectCollectDevice1(criteriaMap);

        List<DevCollectDeviceTypeEntity> devCollectDeviceTypeEntities = this.devCollectDeviceTypeDao.selectByExample(null);
        Map<String, Integer> collectDeviceTypes = new HashMap<String, Integer>();

        if (devCollectDeviceTypeEntities.size() > 0) {
            DevCollectDeviceTypeEntity devCollectDeviceTypeEntity = devCollectDeviceTypeEntities.get(0);
            collectDeviceTypes.put(devCollectDeviceTypeEntity.getOutId(), devCollectDeviceTypeEntity.getId());
        }


        String gprs = this.devOptionsService.getCode("COLLECT_DEVICE_CATEGORY", "GPRS_IOT_GAS_METER");
        String flowMeter = this.devOptionsService.getCode("COLLECT_DEVICE_CATEGORY", "FLOW_METER");
        String loraMeter = this.devOptionsService.getCode("COLLECT_DEVICE_CATEGORY", "LORA_METER");
        String bkMeter = this.devOptionsService.getCode("COLLECT_DEVICE_CATEGORY", "BK_METER");

        for ( Map<String, Object> collectDevice : collectDevices) {
            try {
                String meterId = HzhrTypeUtil.object2string(collectDevice.get("METER_ID"));
                String meterNo = HzhrTypeUtil.object2string(collectDevice.get("METER_NO"));
                String installAddr = HzhrTypeUtil.object2string(collectDevice.get("INSTALL_ADDR"));
                String remoteMeterId = HzhrTypeUtil.object2string(collectDevice.get("REMOTE_METER_ID"));
                String mtId = HzhrTypeUtil.object2string(collectDevice.get("MT_ID"));
                String orgId = HzhrTypeUtil.object2string(collectDevice.get("ORG_ID"));
                String yqdzId = HzhrTypeUtil.object2string(collectDevice.get("YQDZ_ID"));
                String pressureUnit = HzhrTypeUtil.object2string(collectDevice.get("PRESSURE_UNIT"));
                String parentNodeId = HzhrTypeUtil.object2string(collectDevice.get("NODE_ID"));
                String c = collectDevice.get("CREATE_TIME").toString();
                Date installTime = null;
                if (!StringUtils.isEmpty(c)) {
                    installTime = HzhrDateUtil.strToDate(c, "yyyy-MM-dd HH:mm:ss.ms");
                }

                String deptCode = orgId.replace("0000", "9-01");
                String resourceCode = orgId.replace("0000", "01000901");

                DevCollectDeviceExample devCollectDeviceExample = new DevCollectDeviceExample();
                devCollectDeviceExample.createCriteria().andOutIdEqualTo(meterId);
                List<DevCollectDeviceEntity> devCollectDataEntities = this.devCollectDeviceDao.selectByExample(devCollectDeviceExample);

                if (devCollectDataEntities.size() == 0) {
                    DevCollectDeviceEntity devCollectDeviceEntity = new DevCollectDeviceEntity();
                    devCollectDeviceEntity.setOutId(meterId);
                    devCollectDeviceEntity.setCode(meterNo);
                    devCollectDeviceEntity.setDes(installAddr);

                    if ("130".equals(remoteMeterId)) {
                        if (meterNo.startsWith("2066")) {
                            devCollectDeviceEntity.setCategoryCode(bkMeter);
                        } else {
                            devCollectDeviceEntity.setCategoryCode(gprs);
                        }
                    } else if ("132".equals(remoteMeterId)) {
                        devCollectDeviceEntity.setCategoryCode(loraMeter);
                    } else if ("134".equals(remoteMeterId)) {
                        devCollectDeviceEntity.setCategoryCode(flowMeter);
                    }

                    TollGasUserExample example = new TollGasUserExample();
                    example.createCriteria().andOutIdEqualTo(yqdzId);
                    List<TollGasUserEntity> tollGasUserEntities = this.tollGasUserDao.selectByExample(example);
                    if (tollGasUserEntities.size() > 0) {
                        TollGasUserEntity tollGasUserEntity = (TollGasUserEntity)tollGasUserEntities.get(0);
                        devCollectDeviceEntity.setGasUserId(tollGasUserEntity.getId());
                        devCollectDeviceEntity.setGasUserCode(tollGasUserEntity.getCode());
                        devCollectDeviceEntity.setGasUserAddress(tollGasUserEntity.getAddress());
                        devCollectDeviceEntity.setGasUserName(tollGasUserEntity.getName());
                    } else {
                        String resident = this.tollOptionsService.getCode("GAS_USER_TYPE", "RESIDENTIAL_USER");
                        String commercial = this.tollOptionsService.getCode("GAS_USER_TYPE", "COMMERCIAL_USER");


                        Map<String, Object> query = new HashMap<String, Object>();
                        query.put("meterId", meterId);
                        Map<String, Object> gasUser = this.smGasUserCustomDao.selectGasUser1(query);
                        String yqdzId1 = HzhrTypeUtil.object2string(gasUser.get("YQDZ_ID"));
                        String yqdzYqzh = HzhrTypeUtil.object2string(gasUser.get("YQDZ_YQZH"));
                        String yqdzHzMc = HzhrTypeUtil.object2string(gasUser.get("YQDZ_HZ_MC"));
                        String yqdzSm = HzhrTypeUtil.object2string(gasUser.get("YQDZ_SM"));
                        String yqdzKhLx = HzhrTypeUtil.object2string(gasUser.get("YQDZ_KH_LX"));
                        String orgId1 = HzhrTypeUtil.object2string(gasUser.get("ORG_ID"));

                        String deptCode1 = orgId1.replace("0000", "9-01");
                        String resourceCode1 = orgId1.replace("0000", "01000901");

                        TollGasUserExample tollGasUserExample = new TollGasUserExample();
                        tollGasUserExample.createCriteria().andOutIdEqualTo(yqdzId);
                        List<TollGasUserEntity> tollGasUserEntities1 = this.tollGasUserDao.selectByExample(tollGasUserExample);

                        if (tollGasUserEntities1.size() == 0) {
                            TollGasUserEntity tollGasUserEntity = new TollGasUserEntity();
                            tollGasUserEntity.setOutId(yqdzId1);
                            tollGasUserEntity.setCode(yqdzYqzh);
                            tollGasUserEntity.setName((yqdzHzMc != null) ? yqdzHzMc : "未知");
                            tollGasUserEntity.setAddress(yqdzSm);
                            if ("1".equals(yqdzKhLx)) {
                                tollGasUserEntity.setTypeCode(commercial);
                            } else if ("2".equals(yqdzKhLx)) {
                                tollGasUserEntity.setTypeCode(resident);
                            } else {
                                tollGasUserEntity.setTypeCode("0");
                            }
                            tollGasUserEntity.setCompanyId(Integer.valueOf(Integer.parseInt(this.companyId)));

                            UsrDeptExample usrDeptExample = new UsrDeptExample();
                            usrDeptExample.createCriteria().andCodeEqualTo(deptCode1);
                            List<UsrDeptEntity> usrDeptEntities1 = this.usrDeptDao.selectByExample(usrDeptExample);
                            if (usrDeptEntities1.size() > 0) {
                                UsrDeptEntity usrDeptEntity = (UsrDeptEntity)usrDeptEntities1.get(0);
                                tollGasUserEntity.setDeptId(usrDeptEntity.getId());
                            } else {
                                tollGasUserEntity.setDeptId(Integer.valueOf(0));
                            }

                            tollGasUserEntity.setCreateTime(new Date());
                            tollGasUserEntity.setCreateUserId(Integer.valueOf(0));
                            tollGasUserEntity.setResourceCode(resourceCode1);
                            tollGasUserEntity.setIsValid(CommonOptions.YES_NO.YES);
                            tollGasUserEntity.setState("1");
                            this.tollGasUserDao.insert(tollGasUserEntity);

                            devCollectDeviceEntity.setGasUserId(tollGasUserEntity.getId());
                            devCollectDeviceEntity.setGasUserCode(tollGasUserEntity.getCode());
                            devCollectDeviceEntity.setGasUserAddress(tollGasUserEntity.getAddress());
                            devCollectDeviceEntity.setGasUserName(tollGasUserEntity.getName());
                        }
                    }


                    Integer typeId = (Integer)collectDeviceTypes.get(mtId);
                    if (typeId != null) {
                        devCollectDeviceEntity.setTypeId(typeId);
                    } else {
                        devCollectDeviceEntity.setTypeId(Integer.valueOf(0));
                    }


                    Iterator iterator = dtus.iterator();
                    while (iterator.hasNext()) { DevIotDeviceEntity dtu = (DevIotDeviceEntity)iterator.next();
                        if (parentNodeId != null && parentNodeId.equals(dtu.getOutId())) {
                            devCollectDeviceEntity.setIotDeviceId(dtu.getId());
                            break;
                        }
                    }

                    UsrDeptEntity usrDeptEntity = (UsrDeptEntity)userDepts.get(deptCode);
                    if (usrDeptEntity != null) {
                        devCollectDeviceEntity.setDeptId(usrDeptEntity.getId());
                        devCollectDeviceEntity.setDeptCode(usrDeptEntity.getCode());
                    } else {
                        devCollectDeviceEntity.setDeptId(Integer.valueOf(0));
                    }

                    if ("1".equals(pressureUnit)) {
                        devCollectDeviceEntity.setPressureUnit("3");
                    } else if ("2".equals(pressureUnit)) {
                        devCollectDeviceEntity.setPressureUnit("2");
                    } else if ("3".equals(pressureUnit)) {
                        devCollectDeviceEntity.setPressureUnit("1");
                    }

                    devCollectDeviceEntity.setCompanyId(Integer.valueOf(Integer.parseInt(this.companyId)));
                    devCollectDeviceEntity.setInstallTime(installTime);
                    devCollectDeviceEntity.setCreateTime(new Date());
                    devCollectDeviceEntity.setCreateUserId(Integer.valueOf(0));
                    devCollectDeviceEntity.setResourceCode(resourceCode);
                    devCollectDeviceEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devCollectDeviceEntity.setState("1");
                    this.devCollectDeviceDao.insert(devCollectDeviceEntity);
                    flowMetres.add(devCollectDeviceEntity);
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }



        String open = this.devOptionsService.getCode("VALVE_STATUS", "OPEN");
        String close = this.devOptionsService.getCode("VALVE_STATUS", "CLOSE");
        this.devOptionsService.getCode("VALVE_STATUS", "ERROR");
        List<DevCollectDataEntity> addList = new ArrayList<DevCollectDataEntity>();


        Iterator iterator2 = flowMetres.iterator();
        while (iterator2.hasNext()) {
            DevCollectDeviceEntity flowMeter1 = (DevCollectDeviceEntity)iterator2.next();
            criteriaMap = new HashMap<String, Object>();
            criteriaMap.put("startDate", HzhrDateUtil.dateToStr(HzhrDateUtil.getDayBegin()));
            criteriaMap.put("eqId", flowMeter1.getOutId());
            List<Map<String, Object>> collecingDatas = this.smCollectDataCustomDao.selectCollectData1(criteriaMap);

            Iterator iterator = collecingDatas.iterator();
            while (iterator.hasNext()) {
                Map<String, Object> collectingData = (Map)iterator.next();
                HzhrTypeUtil.object2string(collectingData.get("CD_ID"));
                String nodeId = HzhrTypeUtil.object2string(collectingData.get("NODE_ID"));
                if (nodeId == null)
                    continue;
                String eqId = HzhrTypeUtil.object2string(collectingData.get("EQ_ID"));
                if (eqId == null)
                    continue;
                String connectionPointNode = HzhrTypeUtil.object2string(collectingData.get("CONNECTION_POINT_NODE"));
                String cdTime = HzhrTypeUtil.object2string(collectingData.get("CD_TIME"));
                if (cdTime == null)
                    continue;
                cdTime = cdTime.substring(0, cdTime.indexOf("."));
                Date currentTime = HzhrDateUtil.strToDate(cdTime);
                String currentValue = HzhrTypeUtil.object2string(collectingData.get("CURRENT_VALUE"));
                if (currentValue == null)
                    continue;
                String signal = HzhrTypeUtil.object2string(collectingData.get("SIGNAL"));
                HzhrTypeUtil.object2string(collectingData.get("POWERTYPE"));
                String voltage = HzhrTypeUtil.object2string(collectingData.get("VOLTAGE"));
                String tmp = HzhrTypeUtil.object2string(collectingData.get("TMP"));
                String pressure = HzhrTypeUtil.object2string(collectingData.get("PRESSURE"));
                String temperature = HzhrTypeUtil.object2string(collectingData.get("TEMPERATURE"));
                String standardFlow = HzhrTypeUtil.object2string(collectingData.get("STANDARD_FLOW"));
                String workFlow = HzhrTypeUtil.object2string(collectingData.get("WORKING_FLOW"));
                String standardFlowSum = HzhrTypeUtil.object2string(collectingData.get("STANDARD_FLOW_SUM"));
                String workFlowSum = HzhrTypeUtil.object2string(collectingData.get("WORKING_FLOW_SUM"));
                String standardCorrect = HzhrTypeUtil.object2string(collectingData.get("STANDARD_CORRECT"));
                String valveStateId = HzhrTypeUtil.object2string(collectingData.get("VALVE_STATE_ID"));

                DevCollectDataExample devCollectDataExample = new DevCollectDataExample();
                devCollectDataExample.createCriteria().andCollectDeviceIdEqualTo(flowMeter1.getId()).andCurrentTmEqualTo(currentTime);
                int count = this.devCollectDataDao.countByExample(devCollectDataExample);

                if (count == 0) {
                    DevCollectDataEntity devCollectDataEntity = new DevCollectDataEntity();
                    devCollectDataEntity.setTemperature(HzhrTypeUtil.string2float(temperature));
                    devCollectDataEntity.setCollectDeviceId(flowMeter1.getId());

                    Iterator iterator3 = dtus.iterator();
                    while (iterator3.hasNext()) {
                        DevIotDeviceEntity dtu = (DevIotDeviceEntity)iterator3.next();
                        if (connectionPointNode != null && connectionPointNode.equals(dtu.getOutId())) {
                            devCollectDataEntity.setIotDeviceId(dtu.getId());
                            break;
                        }
                    }

                    devCollectDataEntity.setCurrentTm(currentTime);
                    devCollectDataEntity.setCurrentValue(Double.valueOf(Double.parseDouble(currentValue)));
                    devCollectDataEntity.setStaTotalFlow(HzhrTypeUtil.string2double(standardFlowSum));
                    devCollectDataEntity.setWorkTotalFlow(HzhrTypeUtil.string2double(workFlowSum));
                    devCollectDataEntity.setDisTotalFlow(HzhrTypeUtil.string2double(standardCorrect));
                    devCollectDataEntity.setStaFlowRate(HzhrTypeUtil.string2double(standardFlow));
                    devCollectDataEntity.setWorkFlowRate(HzhrTypeUtil.string2double(workFlow));
                    devCollectDataEntity.setPressure(HzhrTypeUtil.string2double(pressure));
                    devCollectDataEntity.setTemperature(HzhrTypeUtil.string2float(tmp));
                    if ("1".equals(valveStateId)) {
                        devCollectDataEntity.setValveStatusCode(open);
                    } else if ("0".equals(valveStateId)) {
                        devCollectDataEntity.setValveStatusCode(close);
                    }
                    devCollectDataEntity.setSignalStrength(HzhrTypeUtil.string2integer(signal));
                    devCollectDataEntity.setCellVoltage(HzhrTypeUtil.string2float(voltage));
                    devCollectDataEntity.setCreateTime(new Date());
                    devCollectDataEntity.setIsValid(CommonOptions.YES_NO.YES);
                    devCollectDataEntity.setState("1");
                    addList.add(devCollectDataEntity);
                    if (addList.size() > 5000) {
                        this.devCollectDataCustomDao.insertBatch(addList);
                        addList = new ArrayList<DevCollectDataEntity>();
                    }
                }
            }
        }

        int addListSize = addList.size();
        if (addListSize > 0)
            this.devCollectDataCustomDao.insertBatch(addList);
    }

    @RequestMapping(value = "/syncCollectingData_5s")
    public void syncCollectingData_5s() throws Exception {
        int syncCount = 0;
        long start = new Date().getTime();

        Date date1 = HzhrDateUtil.getDayBegin();
        Date date2 = HzhrDateUtil.addDay(date1, 1);

        //同步数据
        List<Future<Integer>> lstFuture = new ArrayList<Future<Integer>>();// 存放所有的线程，用于获取结果
        Future<Integer> result = collectingDataService.syncCollectingData1(date1, date2);
        lstFuture.add(result);
        for (Future<Integer> future : lstFuture) {// 获取值。get是阻塞式，等待当前线程完成才返回值
            syncCount += future.get();
        }

        long end = new Date().getTime();
        logger.info("同步采集数据结束: " + (end - start) / 1000 + "s");
        logger.info("同步时间: " + (end - start) / 1000 + "s");
        logger.info("同步数量: " + syncCount + "条");
    }
}
